package com.hcy;

import com.entity.User;
import com.jch.Dao.UserDao;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;

public class Recharge extends JDialog{
	private static final long serialVersionUID = 2389316971270763299L;
	//显示一个小手
	private Cursor hand = new Cursor(Cursor.HAND_CURSOR);


	private JLabel jl_Tips;//提示文本

	private JLabel jl_ten;
	private JTextField jtf_payment;//充值金额输入框

	private JLabel jl_type;

	private JLabel jl_payment;//支付按钮

	public int number;

	private LineBorder huise = new LineBorder(Color.LIGHT_GRAY);
	private LineBorder lanse = new LineBorder(Color.BLUE);

	public Recharge(User user) {

		setLayout(null);

		//文本
		jl_Tips = new JLabel("欢迎进入充值界面",JLabel.CENTER);
		jl_Tips.setFont(new Font("楷体", Font.BOLD, 25));//设置字体大小
		jl_Tips.setForeground(Color.black);//设置字体颜色
		jl_Tips.setBounds(20, 10, 350, 30);//设置位置
		add(jl_Tips);




		//充值
		jl_ten = new JLabel("充值金额：",JLabel.CENTER);
		jl_ten.setBounds(80, 100, 100, 30);
		jl_ten.setFont(new Font("楷体", Font.PLAIN, 18));
		jl_ten.setForeground(Color.black);

		add(jl_ten);

		jtf_payment = new JTextField();
		jtf_payment.setBounds(200,100,100,30);
		//限制输入的只能为数字
		jtf_payment.addKeyListener(new KeyAdapter() {

			@Override
			public void keyTyped(KeyEvent e) {
				char keyChar = e.getKeyChar();
				if (keyChar >= '0' && keyChar<= '9'){

					return;
				}
				Toolkit.getDefaultToolkit().beep();

				e.consume();
			}
		});



		//限制输入长度
		jtf_payment.setDocument( new PlainDocument(){
			@Override
			public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {

				boolean psw= Pattern.matches("^[0-9]*$",jtf_payment.getText());
				String text = jtf_payment.getText();
				if (psw == true){
					if (text.length() + str.length() > 4){
						Toolkit.getDefaultToolkit().beep();
						JOptionPane.showMessageDialog(Recharge.this,"超出4位数！"
								,"错误", JOptionPane.ERROR_MESSAGE);
						return;

					}
				}


				super.insertString(offs, str, a);
			}
		});

		add(jtf_payment);

		//支付充值按钮
		jl_payment = new JLabel(new ImageIcon(Recharge.class.getResource("支付充值按钮.png")));
		jl_payment.setBounds(100, 200, 172, 30);
		jl_payment.setCursor(hand);
		jl_payment.registerKeyboardAction(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				boolean psw= Pattern.matches("^[0-9]*$",jtf_payment.getText());
				if (psw == true){
					if (!jtf_payment.getText().equals("")){
						UserDao userDao = new UserDao();
						int i = userDao.updateAddBalance(jtf_payment.getText(),user.getuName());
						JOptionPane.showMessageDialog(Recharge.this, "充值成功"
								,"正确", JOptionPane.DEFAULT_OPTION);
						dispose();
					}else{

						JOptionPane.showMessageDialog(Recharge.this, "充值失败"
								,"错误", JOptionPane.DEFAULT_OPTION);

					}
				}

			  }
			},KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		jl_payment.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {

				boolean psw= Pattern.matches("^[0-9]*$",jtf_payment.getText());
				if (psw == true){
					if (!jtf_payment.getText().equals("")){
						UserDao userDao = new UserDao();
						int i = userDao.updateAddBalance(jtf_payment.getText(),user.getuName());
						JOptionPane.showMessageDialog(Recharge.this, "充值成功"
								,"正确", JOptionPane.DEFAULT_OPTION);
						dispose();
						System.out.println(i);
					}else{

						JOptionPane.showMessageDialog(Recharge.this, "充值失败"
								,"错误", JOptionPane.DEFAULT_OPTION);

					}
				}
			}
		});

		jl_type = new JLabel("只能输入数字，其他无效");
		jl_type.setBounds(100, 150, 172, 30);
		jl_type.setFont(new Font("楷体", Font.PLAIN, 14));
		jl_type.setForeground(Color.black);
		add(jl_type);


		add(jl_payment);



		setTitle("充值界面");
		setResizable(false);
		setSize(400, 300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);


	}



	public static void main(String[] args) {
//		new Recharge();

	}

}
